<?php
class SubjectsController extends AppController {
    
    public function beforeFilter() {
        $this->Auth->allow('view', 'index');
    }
    
    public function isAuthorized($user) {
        $this->loadModel('User');
        $this->User->id = $user['id'];
        $this->User->unbindModel(array('hasMany' => array('Test')));
        $role = $this->User->read('role');
        if($role['User']['role'] == 'none') {
            if(in_array($this->action, array('view', 'index'))) {
                return true;
            }
        }
        if($role['User']['role'] == 'admin') {
            if(in_array($this->action, array('view'))) {
                return true;
            }
        }
    }
    public function index() {
        $this->set('title_for_layout', 'Thư Viện Đề Phổ Thông Cơ Sở :: ChomoTech');
        $this->loadModel('Subject');
        $subject = $this->Subject->find('all');
        $this->loadModel('Test');
        $this->Test->unbindModel(array('hasMany' => array('Question', 'Like'), 'belongsTo' => array('Subject', 'Grade', 'Training')));
        $i = 0;
        foreach($subject as $sj) {
            $subject[$i]['Subject']['count'] = $this->Test->find('count', array('conditions' => array('subject_id' => $sj['Subject']['id'], 'share' => 1)));
            $i++;
        }
        $this->set('subjectView', $subject);
    }
    
    public function view($id, $grade_id = null) {
        if($id) {
            $this->Subject->id = $id;
            
            if($this->Subject->exists()) {
                $this->Subject->unbindModel(array('hasMany' => array('Test')));
                $subject = $this->Subject->read(array('id', 'name'));
                
                $this->set('title_for_layout', 'Thư viện đề '.$subject['Subject']['name'].' :: ChomoTech');
                $this->set('description', 'Thư viện đề '.$subject['Subject']['name']);
                $this->set('keywords', 'thư viện đề '.$subject['Subject']['name']);
                
                $this->set('subjectView', $subject);
                
                $this->loadModel('Test');
                $this->Test->unbindModel(array('hasMany' => array('Question', 'Like')));
                
                $this->paginate = array(
                'Test' => array(
                    'limit' => 10,
                    'order' => array('like' => 'desc')
                    )
                );
                $this->loadModel('Grade');
                $this->Grade->unbindModel(array('hasMany' => array('Test')));
                $this->set('gradeView', $this->Grade->find('all'));                
        
                if($grade_id) {
                    $this->set('gradeId', $grade_id);
                    $this->set('totalTest', $this->Test->find('count', array('conditions' => array('grade_id' => $grade_id, 'share' => 1, 'subject_id' => $id))));
                    $this->set('testView', $this->paginate('Test', array('Test.share' => 1, 'Test.subject_id' => $id, 'Test.grade_id' => $grade_id)));
                    $grade_name = $this->Grade->find('first', array('fields' => 'name', 'conditions' => array('id' => $grade_id)));
                    $this->set('title_for_layout', 'Thư viện đề '.$subject['Subject']['name'].' '.$grade_name['Grade']['name'].' :: ChomoTech');
                    $this->set('description', 'Thư viện đề '.$subject['Subject']['name'].' '.$grade_name['Grade']['name']);
                    $this->set('keywords', 'thư viện đề '.$subject['Subject']['name'].' '.$grade_name['Grade']['name']);
                } else {
                    $this->set('totalTest', $this->Test->find('count', array('conditions' => array('share' => 1, 'subject_id' => $id))));
                    $this->set('testView', $this->paginate('Test', array('Test.share' => 1, 'Test.subject_id' => $id)));
                }
            } else {
                $this->redirect(array('controller' => 'index'));
            }
        } else {
            $this->redirect(array('controller' => 'index'));
        }
    }
}
?>